package com.jinglan.jybsh.api.impl;

import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jinglan.framework.common.OrderState;
import com.jinglan.jybsh.api.QbJybshChannel;
import com.jinglan.jybsh.service.JybShService;
import com.jinglan.qcoin.center.pojo.QcoinChannelOrder;

/**
 * 
 * @author 测试程序的实现
 *
 */
@Service
public class JybshChannelImpl implements QbJybshChannel, InitializingBean {
	private Logger logger = LoggerFactory.getLogger(JybshChannelImpl.class);
	private Random random = new Random();
	@Autowired
	private JybShService jybShService;

	// Q币下订单
	@Override
	public QcoinChannelOrder placeOrder(boolean testEnv, Integer testOrderState, QcoinChannelOrder channelOrder) {
		String orderNo = channelOrder.getPartnerOrderNo();
		String channelOrderNo = channelOrder.getChannelOrderNo();
		long rechargeAmount = channelOrder.getRechargeAmount();
		String qq = channelOrder.getQq();
		String fixIp = channelOrder.getFixedIp();// 优先使用fixip
		String msgHead = "系统订单[" + orderNo + "]渠道订单[" + channelOrderNo + "]充值号码[" + qq + "]";
		QcoinChannelOrder rstChannelOrder = null;
		if (testEnv) {
			rstChannelOrder = new QcoinChannelOrder();
			rstChannelOrder.setChannelOrderNo(channelOrderNo);
			rstChannelOrder.setOrderState(OrderState.handling.getState());
			rstChannelOrder.setOrderStateDesc(OrderState.handling.getStateDesc());
			rstChannelOrder.setChannelResult("testChannelResult");
			rstChannelOrder.setChannelState("testChannelState");
		} else {
			try {
				rstChannelOrder = jybShService.placeOrder(msgHead, fixIp, channelOrderNo, rechargeAmount, qq, orderNo,channelOrder.getIp());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		logger.info(msgHead + "当前[测试]渠道[" + (testEnv ? "打桩" : "生产") + "]环境[下单]返回结果:" + JSON.toJSONString(rstChannelOrder));
		return rstChannelOrder;
	}

	// 查单
	public QcoinChannelOrder queryOrder(boolean testEnv, Integer testOrderState, QcoinChannelOrder channelOrder) {
		String orderNo = channelOrder.getOrderNo();
		String channelOrderNo = channelOrder.getChannelOrderNo();
		long rechargeAmount = channelOrder.getRechargeAmount();
		String qq = channelOrder.getQq();
		String msgHead = "系统订单[" + orderNo + "]渠道订单[" + channelOrderNo + "]查询号码[" + qq + "]";

		QcoinChannelOrder rstChannelOrder;
		if (testEnv) {
			rstChannelOrder = new QcoinChannelOrder();
			rstChannelOrder.setChannelOrderNo(channelOrderNo);
			rstChannelOrder.setChannelResult("testChannelResult");
			rstChannelOrder.setChannelState("testChannelState");
			if (testOrderState == null) {
				testOrderState = random.nextInt(3);
			}
			switch (testOrderState) {
			case 0:
				rstChannelOrder.setOrderState(OrderState.fail.getState());
				rstChannelOrder.setOrderStateDesc(OrderState.fail.getStateDesc());
				rstChannelOrder.setSuccessAmount(0L);
				break;
			case 1:
				rstChannelOrder.setOrderState(OrderState.success.getState());
				rstChannelOrder.setOrderStateDesc(OrderState.success.getStateDesc());
				rstChannelOrder.setSuccessAmount(rechargeAmount);
				break;
			default:
				rstChannelOrder.setOrderState(OrderState.unsure.getState());
				rstChannelOrder.setOrderStateDesc(OrderState.unsure.getStateDesc());
				break;
			}
		} else {
			rstChannelOrder = jybShService.queryOrder(msgHead, qq, channelOrderNo,
					channelOrder.getPartnerOrderNo());
		}
		logger.info(
				msgHead + "当前[测试]渠道[" + (testEnv ? "打桩" : "生产") + "]环境[查单]返回结果:" + JSON.toJSONString(rstChannelOrder));
		return rstChannelOrder;
	}

	@Override
	public String queryBalance(boolean testEnv) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		try {
			QcoinChannelOrder rstChannelOrder = new QcoinChannelOrder();
			rstChannelOrder.setChannelOrderNo("1235567902244");
			rstChannelOrder.setPartnerOrderNo("");
			rstChannelOrder.setIp("192.168.0.1");
			//rstChannelOrder.setOrderNo("2123112245");
			rstChannelOrder.setRechargeAmount((long) 30);
			rstChannelOrder.setQq("995731349");
			Integer testOrderState = 1;
			// 下单测试
			//placeOrder(false, testOrderState, rstChannelOrder);
			// 查单测试
			 queryOrder(false, testOrderState, rstChannelOrder);
		} catch (Exception e) {
			logger.info("测试44" + e);
		}

	}

}
